package com.dongqi.actionRecord.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dongqi.actionRecord.dto.ActionRecordDetailDTO;
import com.dongqi.actionRecord.entity.ActionRecordDetailEntity;
import com.dongqi.actionRecord.mapper.ActionRecordDetailMapper;
import com.dongqi.actionRecord.service.ActionRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
@Slf4j
public class ActionRecordServiceImpl implements ActionRecordService {

    @Autowired
    private ActionRecordDetailMapper actionRecordDetailMapper;

    @Override
    public Map<String, Object> queryAll() {
        HashMap<String, Object> map = new HashMap<>();
        List<ActionRecordDetailEntity> robbotEntities = actionRecordDetailMapper.selectRobot(null);
        List<ActionRecordDetailEntity> vehicleEntities = actionRecordDetailMapper.selectVehicle(null);
        List<ActionRecordDetailDTO> robotList = new ArrayList<>();
        List<ActionRecordDetailDTO> vehicleList = new ArrayList<>();
        if (robbotEntities.size() > 0) {
            for (ActionRecordDetailEntity robotEntity : robbotEntities) {
                ActionRecordDetailDTO actionRecordDetailDTO = new ActionRecordDetailDTO();
                BeanUtils.copyProperties(robotEntity, actionRecordDetailDTO);
                robotList.add(actionRecordDetailDTO);
            }
        }
        if (vehicleList.size() > 0) {
            for (ActionRecordDetailEntity vehicleEntity : vehicleEntities) {
                ActionRecordDetailDTO actionRecordDetailDTO = new ActionRecordDetailDTO();
                BeanUtils.copyProperties(vehicleEntity, actionRecordDetailDTO);
                robotList.add(actionRecordDetailDTO);
            }

        }
        map.put("robot",robbotEntities);
        map.put("vehicle",vehicleEntities);
        return map;
    }

    @Override
    public List<ActionRecordDetailDTO> queryByBehaviorType(String behaviorType) {
        QueryWrapper<ActionRecordDetailEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("behavior_type", behaviorType);
        List<ActionRecordDetailEntity> actionRecordDetailEntities = actionRecordDetailMapper.selectList(wrapper);
        if (actionRecordDetailEntities.size() > 0) {
            List<ActionRecordDetailDTO> arrayList = new ArrayList<>();
            for (ActionRecordDetailEntity actionRecordDetailEntity : actionRecordDetailEntities) {
                ActionRecordDetailDTO actionRecordDetailDTO = new ActionRecordDetailDTO();
                BeanUtils.copyProperties(actionRecordDetailEntity, actionRecordDetailDTO);
                arrayList.add(actionRecordDetailDTO);
            }
            return arrayList;
        } else {
            return null;
        }
    }
}
